package com.itheima.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author wz
 * @description:
 * @date 2021-11-01
 */
public class CarProxy {
    public static void main(String[] args) {
        Car proxy = (Car) Proxy.newProxyInstance(
                Car.class.getClassLoader(),
                // 自己组装接口的字节码数组
                new Class[]{Car.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        String methodName = method.getName();
                        if (methodName.equals("run")) {
                            System.out.println("行车注意安全");
                        }
                        return null;
                    }
                }
        );

        proxy.run();
    }
}
